package com.ruoyi.warning.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.sale.domain.SaleorderOrder;
import com.ruoyi.slow.mapper.SlowMapper;
import com.ruoyi.warning.domain.Warning;
import com.ruoyi.warning.mapper.WarningMapper;
import com.ruoyi.warning.service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每月预警值Service业务层处理
 *
 * @author zjh
 * @date 2022-06-09
 */
@Service
public class WarningServiceImpl implements WarningService
{
    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private SlowMapper slowMapper;

    /**
     * 查询每月预警值
     *
     * @param warningId 每月预警值ID
     * @return 每月预警值
     */
    @Override
    public Warning selectWarningById(Long warningId)
    {
        return warningMapper.selectWarningById(warningId);
    }

    /**
     * 查询每月预警值列表
     *
     * @param warning 每月预警值
     * @return 每月预警值
     */
    @Override
    public List<Warning> selectWarningList(Warning warning)
    {
        return warningMapper.selectWarningList(warning);
    }

    /**
     * 查询每月预警值列表
     *
     * @param warning 每月预警值
     * @return 每月预警值
     */
    @Override
    public List<Warning> selectWarningListWhichLack(Warning warning)
    {
        warning.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        return warningMapper.selectWarningListWhichLack(warning);
    }

    /**
     * 查询每月产品列表
     *
     * @return 每月产品销售数量集合
     */
    @Override
    public List<Warning> selectWarningListToEach()
    {
        return warningMapper.selectWarningListToEach();
    }

    /**
     * 查询未支付订单数
     *
     * @return 结果
     */
    @Override
    public int selectPayCount()
    {
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        SaleorderOrder saleorderOrder=new SaleorderOrder();
        saleorderOrder.setDeptId(sysUser.getCompanyId());
        if (!sysUser.getDeptId().equals(sysUser.getCompanyId())){
            saleorderOrder.setSaleorderUserId(sysUser.getUserId());
        }
        return warningMapper.selectPayCount(saleorderOrder);
    }

    /**
     * 查询未开票数
     *
     * @return 结果
     */
    @Override
    public int selectInvoiceCount()
    {
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        SaleorderOrder saleorderOrder=new SaleorderOrder();
         if (!sysUser.getDeptId().equals(sysUser.getCompanyId())){
            saleorderOrder.setSaleorderUserId(sysUser.getUserId());
        }
        return warningMapper.selectInvoiceCount(saleorderOrder);
    }

    /**
     * 新增每月预警值
     *
     * @param warning 每月预警值
     * @return 结果
     */
    @Override
    public int insertWarning(Warning warning)
    {
        warning.setCreateTime(DateUtils.getNowDate());
        return warningMapper.insertWarning(warning);
    }

    /**
     * 新增每月预警值
     *
     * @return 结果
     */
    @Override
    public int insertWarnings()
    {
        List<Warning> warnings=warningMapper.selectWarningListToEach();
        List<ProductModel> productModelList=slowMapper.selectSlowListTwo(new ProductModel());
        List<Warning> warningList=new ArrayList<>();
        Date nowDate= DateUtils.getNowDate();
        for(Warning warning:warnings){
            for(ProductModel productModel:productModelList){
                if(warning.getCompanyId().equals(productModel.getDeptId())&& warning.getProductId().equals(productModel.getProductId())&& warning.getProductType().equals(productModel.getProductType())){
                    int ratio=getMonth(productModel.getCreateTime(),nowDate);
                    if(ratio>6){
                        ratio=6;
                    }
                    if(ratio!=0){
                        Long num=warning.getWarningNum()/ratio;
                        warning.setWarningNum(num);
                    }
                    warningList.add(warning);
                }
            }
        }
        return warningMapper.insertWarnings(warningList);
    }

    public  int getMonth(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);
        int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        if ((startCalendar.get(Calendar.DATE) == 1)&& (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }

    /**
     * 修改每月预警值
     *
     * @param warning 每月预警值
     * @return 结果
     */
    @Override
    public int updateWarning(Warning warning)
    {
        warning.setUpdateTime(DateUtils.getNowDate());
        return warningMapper.updateWarning(warning);
    }

    /**
     * 删除每月预警值对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWarningByIds(String ids)
    {
        return warningMapper.deleteWarningByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除每月预警值信息
     *
     * @param warningId 每月预警值ID
     * @return 结果
     */
    @Override
    public int deleteWarningById(Long warningId)
    {
        return warningMapper.deleteWarningById(warningId);
    }
}
